package models

import (
	"github.com/pkg/errors"
)

/*type TManageUserInfo struct {
	Id          int
	Username    string    `xorm:"varchar(32) not null 'username'" json:"username"`
	Password    string    `xorm:"varchar(32) notnull 'password'" json:"password"`
	Email       string    `xorm:"varchar(32) notnull 'email'" json:"email"`
	Mobile      string    `xorm:"varchar(12) notnull 'mobile'" json:"mobile"`
	Stauts      int       `xorm:"'status'" json:"status"`
	Type        int       `xorm:"type" json:"type"`
	Updateon    time.Time `xorm:"updateon" json:"updateon"`
	Createon    time.Time `xorm:"createon" json:"createon"`
	AccessToken string    `xorm:"access_token" json:"access_token"`
}*/

func GetAllManage() ([]TManageUserInfo, error) {
	manages := make([]TManageUserInfo, 0)
	err := configEngine.Where("status = ?", 0).Find(manages)
	if err != nil {
		return nil, err
	}
	return manages, nil
}

func GetManageById(id int) (*TManageUserInfo, error) {
	manage := new(TManageUserInfo)
	_, err := configEngine.Id(id).Get(manage)
	if err != nil {
		return manage, err
	}
	return manage, nil
}

func GetManageByName(username string) (*TManageUserInfo, error) {
	manage := &TManageUserInfo{Username: username}
	has, err := configEngine.Get(manage)
	if err != nil {
		return manage, err
	}
	if !has {
		return manage, errors.New("查询信息不存在")
	}
	return manage, nil
}

func GetManageByToken(token string) (*TManageUserInfo, error) {
	manage := &TManageUserInfo{AccessToken: token}
	has, err := configEngine.Get(manage)
	if err != nil {
		return manage, err
	}
	if !has {
		return manage, errors.New("查询的用户不存在")
	}
	return manage, nil
}

func MakeNewToken(id int64, token string) error {
	manage := &TManageUserInfo{AccessToken: token}
	_, err := configEngine.Where("id=?", id).Cols("access_token").Update(manage)
	return err
}
